Add Sleep Sort Algorithm in R#240
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a Sleep Sort implementation to the repository, a novelty sorting algorithm that sorts elements by launching parallel tasks that sleep for durations proportional to each element's value. The algorithm uses R's parallel package with PSOCK clusters and file modification times to capture completion order.
Key Changes:
- Implements sleep sort with support for negative numbers via offsetting
- Uses parallel execution with configurable workers and adjustable sleep scaling
- Includes fallback to base
sort()if parallel execution fails
| } | ||
|
|
||
| # Prepare an output directory to record completion times | ||
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) |
There was a problem hiding this comment.
The random directory name generation could produce collisions if multiple instances run simultaneously. Use tempfile() instead, which guarantees unique temporary paths: outdir <- tempfile(pattern = 'sleep_sort_').
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) | |
| outdir <- tempfile(pattern = "sleep_sort_") |
|
|
||
| # Prepare an output directory to record completion times | ||
| outdir <- file.path(tempdir(), paste0("sleep_sort_", as.integer(runif(1) * 1e9))) | ||
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) |
There was a problem hiding this comment.
The temporary directory is never cleaned up after function execution. Add on.exit(unlink(outdir, recursive = TRUE), add = TRUE) after line 39 to ensure cleanup even if an error occurs.
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| dir.create(outdir, recursive = TRUE, showWarnings = FALSE) | |
| on.exit(unlink(outdir, recursive = TRUE), add = TRUE) |
| if (length(files) != n) { | ||
| # In rare cases, the filesystem may lag; simple retry loop | ||
| tries <- 0 | ||
| while (tries < 50 && length(files) != n) { |
There was a problem hiding this comment.
The magic number 50 for retry attempts lacks context. Extract this as a named constant (e.g., max_retries <- 50) to clarify its purpose and make it easier to adjust.
| while (tries < 50 && length(files) != n) { | |
| max_retries <- 50 | |
| while (tries < max_retries && length(files) != n) { |
|
Please address comments |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
|
This PR was closed because it has been stalled for 7 days with no activity. |
This PR introduces Sleep Sort, a playful and educational sorting algorithm implemented in R.
Sleep Sort launches one task per element, sleeps for a duration proportional to the element's value, and outputs the value when the task completes. The values appear in ascending order based on their "wake-up" times.
Overview
parallelpackageFeatures
scaleparameter to control sleep durationsort()if parallel execution failsComplexity
scale